// pages/buynow/buynow.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    phone: "",
    address: "",
    sid: '',
    uid: '',
    goods: '',
    car: '',
    remark: '',
    guige: '',
    num: '',
    shopnum: '',
    options: '',
    type: '',
    // username:'',
    phone:'',
  


  },


  chooseAddress1: function() {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  // 调用微信地址
  // chooseAddress() {
  //   let that = this
  //   wx.chooseAddress({
  //     success(res) {
  //       that.setData({
  //         name: res.userName,
  //         phone: res.telNumber,
  //         address: res.provinceName + res.cityName + res.countyName + res.detailInfo
  //       })
  //       console.log(res.userName)
  //       console.log(res.postalCode)
  //       console.log(res.provinceName)
  //       console.log(res.cityName)
  //       console.log(res.countyName)
  //       console.log(res.detailInfo)
  //       console.log(res.nationalCode)
  //       console.log(res.telNumber)
  //     }
  //   })
  // },
  // 备注
  remark: function(e) {
    this.setData({
      remark: e.detail.value
    })
    console.log(this.data.remark)
  },
  // 去付款
  pay() {
    let that = this
    // 创建订单
    let uid=that.data.uid
    console.log('用户身份', uid) 
    let address=that.data.address
    let username=that.data.username
    let phone = that.data.phone
    let addressid= that.data.addressid
    let bean=that.data.bean
    console.log(that.data.address, that.data.username, that.data.phone, that.data.bean)
    wx.request({
      url: app.globalData.api + "addorder",
      data: {
        uid: that.data.uid,
        sid: that.data.sid,
        addressid:that.data.addressid,
        bean: that.data.bean,
        // address: that.data.address,
        phone: that.data.phone,
        username: that.data.username,
        note: that.data.remark,
        price: that.data.total,
        total: that.data.price,
        num: that.data.car.length,
        yunfei: that.data.yunfei,
        goods: that.data.goods,
        bean: that.data.bean
      },

      success: function(res) {
        
        console.log('下单', res)
        if (res.data.code == 0) {
          let oid = res.data.oid
          wx.request({
            // https://bzl.isoft.mobi/index.php/Home/Api/
            url: 'https://bzl.isoft.mobi/index.php/Home/Pay/pay',
              // url: 'http://newbzl.didu86.com/index.php/Home/Pay/pay',

            data: {
              uid: that.data.uid,
              oid: res.data.oid,
              price: that.data.total
            },
            success: function(res) {
              console.log('支付', res)
              let json = res.data
              // console.log(json.appId, json.timeStamp, json.nonceStr, json.package, json.paySign)
              wx.requestPayment({
                appId: json.appId,
                timeStamp: json.timeStamp,
                nonceStr: json.nonceStr,
                package: json.package,
                signType: 'MD5',
                paySign: json.paySign,
                success: function(res) {
                  console.log(res)
                  wx.redirectTo({
                    url: '/pages/buynow/buysuccess/buysuccess?show=' + 1 + '&price=' + that.data.total + '&id=' + oid,
                  })
                },
                fail: function(res) {
                  console.log(res)
                  wx.redirectTo({
                    url: '/pages/buynow/buysuccess/buysuccess?show=' + 2 + '&price=' + that.data.total + '&id=' + oid,
                  })
                }
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
    })

    // wx.navigateTo({
    //   url: '/pages/buynow/buysuccess/buysuccess',
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('传值', options)
    let data = decodeURIComponent(options.goods)
    let car = JSON.parse(data)
    console.log(car)
    this.setData({
      options: options,
      bean: options.bean,
      sid: options.sid,
      uid: options.uid,
      goods: car,
      total: options.total,
      yunfei: options.yunfei,
      price: options.price,
      car: car,
      shopnum: options.num,
      guige: options.guige
    })
    console.log(options.bean, options.price)

    var that = this;
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        console.log('用户身份', res, res.data.id)
        let uid = res.data.id
        that.setData({
          uid: uid

        })

      },
    })
    // console.log(JSON.parse(this.data.goods))
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log(res.data.id)
        let uid = res.data.id
        that.setData({
          uid: uid

        })
        wx.request({
          url: app.globalData.api + 'getAddress', //我的收货地址列表
          data: {
            uid: that.data.uid,
            page: 1,
            limit: 1
          },
          success: function(res) {
            console.log(res)
            var result = res.data.list[0]
            console.log(result);
            let address = result.address_provinces_title + result.address_city_title + result.address_areas_title + result.address_title
            that.setData({
              username: result.username,
              phone: result.phone,
              address: address,
              addressid: result.id,
            })
            console.log(that.data.username, that.data.phone, that.data.address, that.data.addressid)

            // if (result == user.id) {

            // } else {
            //   that.setData({
            //     allAddress: result
            //   });
            // }

          }
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})